package com.rrd.itext.xmlBean.layoutBean;

import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfTransparencyGroup;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.utils.StyleUtil;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementRender;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Circle")
public class XmlCircle extends XmlColorStyle implements PdfElementRender{
	@XStreamAsAttribute
	private int radius;
	@XStreamAsAttribute
	private int offsetX;
	@XStreamAsAttribute
	private int offsetY;
	
	@XStreamAlias("Opacity")
	private float Opacity;
	@XStreamAlias("BlendMode")
	private String BlendMode;
	public int getRadius() {
		return radius;
	}
	public void setRadius(int r) {
		this.radius = r;
	}
	public int getOffsetX() {
		return offsetX;
	}
	public void setOffsetX(int offsetX) {
		this.offsetX = offsetX;
	}
	public int getOffsetY() {
		return offsetY;
	}
	public void setOffsetY(int offsetY) {
		this.offsetY = offsetY;
	}
	public float getOpacity() {
		return Opacity;
	}
	public void setOpacity(float opacity) {
		Opacity = opacity;
	}
	public PdfName getBlendMode() {
		if("HardLight".equals(BlendMode)){
			return new PdfName("HardLight");
		}
		return null;
	}
	public void setBlendMode(String blendMode) {
		BlendMode = blendMode;
	}
    @Override
    public void render(PdfContentByte cb, PDFBuilder builder) throws Exception {
        cb.saveState();
        PdfName blendMode = this.getBlendMode();
        XmlColor color = StyleUtil.createColor(this, builder.getStyleMap());
        int colorC = (int)(((color.getFontColorC()==null?0:color.getFontColorC())/100F)*255);
        int colorM = (int)(((color.getFontColorM()==null?0:color.getFontColorM())/100F)*255);
        int colorY = (int)(((color.getFontColorY()==null?0:color.getFontColorY())/100F)*255);
        int colorK = (int)(((color.getFontColorK()==null?0:color.getFontColorK())/100F)*255);
        if (blendMode != null) {
            PdfTemplate tp = cb.createTemplate(this.getRadius() * 2, this.getRadius() * 2);
            PdfTransparencyGroup group = new PdfTransparencyGroup();
            tp.setGroup(group);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(this.getOpacity());
            gs.setBlendMode(this.getBlendMode());
            tp.setGState(gs);
            cb.setCMYKColorFill(colorC, colorM, colorY,colorK);
            cb.setCMYKColorStroke(colorC, colorM, colorY,colorK);
            tp.circle(this.getRadius(), this.getRadius(), this.getRadius());
            tp.fill();
            cb.addTemplate(tp, this.getOffsetX(), this.getOffsetY());
        } else {
            cb.setCMYKColorFill(colorC, colorM, colorY,colorK);
            cb.setCMYKColorStroke(colorC, colorM, colorY,colorK);
            cb.circle(this.getOffsetX() + this.getRadius(), this.getOffsetY() + this.getRadius(), this.getRadius());
            cb.fill();
        }
        cb.restoreState();
    }
}
